<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        #member-app .notAdmin::after {
            border: 0 !important;
        }

        .tableBox {
            text-align-last: center;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <!-- 搜索部分 -->
        <div class="tableBar">
            <el-input
                    v-model="input"
                    placeholder="请输入会员姓名或手机号"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
            <el-button
                    type="primary"
                    @click="addMemberHandle('add')"
            >
                + 添加会员
            </el-button>
        </div>
        <!-- 表结构 -->
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
                align="center"
        >
            <el-table-column
                    prop="memberName"
                    label="会员姓名"
            ></el-table-column>
            <el-table-column label="性别">
                <template slot-scope="scope">
                    {{ String(scope.row.sex) === '0' ? '女' : '男' }}
                </template>
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="手机号"
            ></el-table-column>
            <el-table-column
                    prop="balance"
                    label="余额"
            ></el-table-column>
            <el-table-column
                    prop="updateTime"
                    label="最后消费时间"
            ></el-table-column>
            <el-table-column
                    label="操作"
                    width="220"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="toClearing(scope.row)"
                            :class="{notAdmin:user !== 'admin'}"
                    >
                        操作
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="addMemberHandle(scope.row.phone)"
                            :class="{notAdmin:user !== 'admin'}"
                    >
                        修改
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="getCostRecord(scope.row.phone)">
                        详情
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="deleteHandle('单删',scope.row.phone)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 弹窗 -->
        <el-dialog
                :title="costData.title"
                :visible.sync="costData.dialogVisible"
                width="30%"
                :before-close="handleClose"
                :modal-append-to-body="false"
        >
            <el-form
                    class="demo-form-inline"
                    label-width="100px"
            >
                <el-form-item label="会员姓名：">
                    <el-input
                            v-model="costData.memberName"
                            :readonly=true
                            maxlength="14"
                    />
                </el-form-item>
                <el-form-item
                        label="业务金额："
                        prop="balance"
                >
                    <el-input
                            v-model="costData.costAmount"
                            placeholder="请输入金额"
                            maxlength="14"
                    />
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input
                            v-model="costData.remark"
                            placeholder="请输入备注"
                            maxlength="14"
                    />
                </el-form-item>
            </el-form>
            <span
                    slot="footer"
                    class="dialog-footer"
            >
                <el-button
                        size="medium"
                        @click="costData.dialogVisible = false">
                    取 消
                </el-button>
                <el-button
                        type="primary"
                        size="medium"
                        @click="submitForm(1)">
                    充 值
                </el-button>
                <el-button
                        type="primary"
                        size="medium"
                        class="continue"
                        @click="submitForm(2)">
                    扣 款
                </el-button>
      </span>
        </el-dialog>
        <!-- 分页组件 -->
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vueMax.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/costAndMember.js"></script>
<script src="../../api/costRecord.js"></script>

<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                id: '',
                sex: '',
                status: true,
                memberData: {
                    'balance': '',
                    'birthday': '',
                    'createTime': '',
                    'createUser': '',
                    'id': '',
                    'memberName': '',
                    'phone': '',
                    'sex': '',
                    'updateTime': '',
                    'updateUser': '',
                },
                costData:
                    {
                        'title': '充值消费',
                        'dialogVisible': false,
                        'phone': '',
                        'memberName': '',
                        'costAmount': '',
                        'bsType': '',
                        'remark': ''
                    }
            }
        },
        computed: {
            rules() {
                return {
                    'balance': [
                        {'required': true, 'message': '请输入金额', 'trigger': 'blur'}
                    ],
                    // 'remark': [{'required': true, 'message': '请输入使用项', 'trigger': 'blur'}],
                }
            }
        },
        created() {
            this.init()
            this.user = JSON.parse(localStorage.getItem('userInfo')).username
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined,
                    status: this.checkInputWithRegex(this.input ? this.input : undefined)
                }
                await getPetShopMemberPage(params).then(res => {

                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addMemberHandle(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '7',
                        url: '/backend/page/member/add.html',
                        name: '添加会员'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '7',
                        url: '/backend/page/member/edit.html?id=' + st,
                        name: '修改会员信息'
                    }, true)
                }
            },
            // 跳转消费充值记录
            getCostRecord(rId) {
                window.parent.menuHandle({
                    id: '7',
                    url: '/backend/page/consumption/costDetail.html?id=' + rId,
                    name: '消费充值记录详情'
                }, true)
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            // 判断输入是查询条件为手机号或姓名
            checkInputWithRegex(name) {
                if (name === undefined) {
                    return 1
                } else {
                    const num = Number(name);
                    return !isNaN(num) && isFinite(name) ? 2 : 3;
                }
            },
            // 充值消费业务
            toClearing(st) {
                this.costData.phone = st.phone
                this.costData.memberName = st.memberName
                this.costData.costAmount = ''
                this.costData.remark = ''
                this.costData.dialogVisible = true
            },
            // 提交消费充值业务
            submitForm(bsType) {
                this.costData.bsType = bsType
                const costData = this.costData
                const valid = (costData.costAmount === 0 || costData.costAmount)
                if (valid) {
                    const reg = /^-?\d+(\.\d+)?$/
                    // 判断输入金额是否为数字
                    if (reg.test(costData.costAmount)) {
                        addMemberRecord({
                            'phone': costData.phone,
                            'memberName': costData.memberName,
                            'costAmount': costData.costAmount,
                            'bsType': costData.bsType,
                            'remark': costData.remark
                        }).then(res => {
                            if (res.code === 1) {
                                this.$message.success('操作成功！')
                                this.costData.dialogVisible = false
                                this.handleQuery()
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                    } else {
                        this.$message.error('只能输入数字类型')
                    }
                } else {
                    this.$message.error('请输入金额！')
                }
            },
            // 关闭弹窗
            handleClose() {
                this.costData.dialogVisible = false
            },
            // 删除
            deleteHandle(type, id) {
                if (type === '批量' && id === null) {
                    if (this.checkList.length === 0) {
                        return this.$message.error('请选择删除对象')
                    }
                }
                this.$confirm('删除该会员将关联删除与改会员绑定的宠物, 是否继续?', '确定删除', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                }).then(() => {
                    deleteMembers(type === '批量' ? this.checkList.join(',') : id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
        }
    })
</script>
</body>
</html>
